package com.bd.framework.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.bd.framework.utils.BaseException;

/***
 * @author hnxdyl@hotmail.com
 * 返回本地SQlite数据库的Connection,注意此类在SQLite实例化Connection
 * 默认SQLite的路径:项目的conf目录下
 * 此单例类实例化SQLiteConnection,在不同地方使用SQLiteConnection时,切记不需要关闭SQLiteConnection,只需要关闭
 * 有Connection创建的Statement。
 * * */
public class SQlite2Connection {
	private Connection SQLiteCon = null;
	private static SQlite2Connection sqliteCon;
	private SQlite2Connection(){
		try {
			Class.forName("org.sqlite.JDBC");
			try {
				SQLiteCon = DriverManager.getConnection("jdbc:sqlite://D:/sqlite/test.db");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(SQLiteCon == null){
				throw new BaseException("SQLiteConnection create error!");
			}
		}
	}
	
	public  Connection getSQLiteCon(){
		return SQLiteCon;
	}
	
	public static SQlite2Connection getSQliteConnection(){
		if(sqliteCon == null){
			sqliteCon = new SQlite2Connection();
		}
		return sqliteCon;
	}
	
	public static void main(String[] args){
		System.out.println(SQlite2Connection.getSQliteConnection().getSQLiteCon());
		System.out.println(SQlite2Connection.getSQliteConnection().getSQLiteCon());
	}
}
